#include <vector>

using namespace std;

class Solution {
public:
    //一维数组的动态和
    vector<int> runningSum(vector<int>& nums) {
        vector<int> ans;
        ans.reserve(nums.size());
        int tmp = 0;
        for (auto num : nums)
        {
            tmp += num;
            ans.push_back(tmp);
        }
        return ans;
    }

    //搜索插入位置
    int searchInsert(vector<int>& nums, int target) {
        int left = 0;
        int right = nums.size()-1;
        int ans = ((target <= nums[left]) ? left : left + 1);;

        while (left < right)
        {
            int mid = (left + right) >> 1;
            if (nums[mid] < target)
            {
                ans = ((target > nums[right]) ? right+1 : right);
                left = mid + 1;
            }
            else if (nums[mid] > target)
            {
                ans = ((target <= nums[left]) ? left : left + 1);
                right = mid - 1;
            }
            else if (nums[mid] == target)
            {
                return mid;
            }
        }
        return ans;
    }
};